package com.kly.cashmall.module.home.home_tab.tab_other;


import androidx.annotation.NonNull;

import com.kly.cashmall.utils.data.TextUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author
 * @date 2018/1/18
 */
public class ObserverFactory {

  private static Map<String, ItemObserver> sObserveMap = new ConcurrentHashMap<>();

  public static <T> ItemObserver<T> newInstance(@NonNull String name) {
    ItemObserver itemObserve = sObserveMap.get(name);
    if (itemObserve == null) {
      itemObserve = new ItemObserver<T>(name);
      sObserveMap.put(name, itemObserve);
    }
    return itemObserve;
  }

  public static void unregister(String code) {
    for (Map.Entry<String, ItemObserver> entry : sObserveMap.entrySet()) {
      if (entry.getValue() != null) {
        entry.getValue().unregisterObserve(code);
      }
    }
  }

  public static void removeObserver(String key) {
    if (sObserveMap != null && !TextUtil.isEmpty(key)) {
      sObserveMap.remove(key);
    }
  }
}
